package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.BaseStudentDao;
import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.util.StudentFactory;

public class StudentService {
//    static OtherStudentDao studentDao = new OtherStudentDao();
    BaseStudentDao studentDao = StudentFactory.getStudentDao();

    public boolean addStudent(Student student) {
        return studentDao.addStudent(student);
    }

    public Student[] findAllStudent() {
        Student[] stuArray = studentDao.findAllStudent();
        int num = 0;
        for (int i = 0; i < stuArray.length; i++) {
            if (stuArray[i] != null){
                num++;
            }
        }
        if (num != 0){
            return stuArray;
        }else {
            return null;
        }
    }

    public boolean isExist(String id) {
        Student[] students = studentDao.findAllStudent();
        boolean flag = false;
        for (int i = 0; i < students.length; i++) {
            if (students[i] != null && students[i].getId().equals(id)){//存在
                flag = true;
            }else {

            }
        }
        return flag;
    }

    public void deleteById(String id) {
        studentDao.deleteById(id);
    }

    public void updateStudent(Student student) {
        studentDao.updateStudent(student);
    }

    public boolean studentExist() {
        Student[] allStudent = studentDao.findAllStudent();
        int num = 0;
        for (int i = 0; i < allStudent.length; i++) {
            if (allStudent[i] != null){
                num++;
            }
        }
        if (num != 0){
            return true;
        }else {
            return false;
        }
    }
}
